/**
 * Copyright (C) 2009 Fsti Inc.
 *
 */
package com.fsti.ppmw.port.payment;

import java.util.Map;

import org.apache.commons.lang.StringUtils;


/**
 * 验证
 * 
 * 
 * @author Song Qing
 *
 */
class Validation {
	
	private static final String validateMessage = "参数值不合法！";
	/**
	 * 验证
	 * @param configParams 配置参数
	 * @param requestParams 请求参数
	 * @throws IllegalArgumentException		请求参数不合法
	 */
	static void Validate(ConfigParams configParams,RequestParams requestParams){
		
		if(requestParams==null){
			throw new IllegalArgumentException("请求参数不能为空！requestParams，实际值："+requestParams);
		}
		
		//abilityType：必须为abilityTypes中的值
		String abilityType=requestParams.getAbilityType();
		Map<String,String> abilityTypes=configParams.getAbilityTypes();
		if (StringUtils.isBlank(abilityType) || !abilityTypes.containsKey(abilityType)) {
			throw new IllegalArgumentException(validateMessage + "abilityType，要求：必须为" + abilityTypes.keySet()
					+ "中的值；实际值：" + abilityType);
		}
		
		//timeStamp：格式必须为yyyymmddhhmmss
		String timeStamp=requestParams.getTimeStamp();
		if (StringUtils.isBlank(timeStamp) || timeStamp.length() != "yyyymmddhhmmss".length()) {
			throw new IllegalArgumentException(validateMessage + "timeStamp，要求：格式必须为yyyymmddhhmmss；实际值：" + timeStamp);
		}
		
		//areaCode：以0开头
		String areaCode=requestParams.getAreaCode();
		if (StringUtils.isBlank(areaCode) || !areaCode.startsWith("0")) {
			throw new IllegalArgumentException(validateMessage + "areaCode，要求：以0开头；实际值：" + areaCode);
		}
		
		//payorgId：必须为payModes或者payorgIds的Key值
		String payorgId=requestParams.getPayorgId();
		Map<String,String> payorgIds=configParams.getPayorgIds();
		Map<String,String> payModes=configParams.getPayModes();
		if (StringUtils.isBlank(payorgId) || !payorgIds.containsKey(payorgId) || !payModes.containsKey(payorgId)) {
			throw new IllegalArgumentException(validateMessage + "payorgId，要求：必须为" + payorgIds.keySet() + "中的值或者必须为"
					+ payModes.keySet() + "中的值；实际值：" + payorgId);
		}
		
		//userTel不能为空
		notEmpty("userTel",requestParams.getUserTel());
		
		//password不能为空
		notEmpty("password",requestParams.getPassword());
		
		//apUser不能为空
		notEmpty("apUser",requestParams.getApUser());
		
		//requestId不能为空
		notEmpty("requestId",requestParams.getRequestId());
		
	}

	/**
	 * 不能为空
	 * 
	 * @param name 		参数名称
	 * @param value 	参数值
	 * @throws IllegalArgumentException 待判断字符为空或者NULL
	 */
	private static void notEmpty(String name,String value) {
		if(StringUtils.isBlank(value)){
			throw new IllegalArgumentException(validateMessage + name+"，要求：不能为空；实际值：" + value);
		}
	}
}
